package com.yxzr.youyou;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yxzr.youyou.bean.UserInfoBean;
import com.yxzr.youyou.fragment.HomeFragment;
import com.yxzr.youyou.fragment.MyFragment;
import com.yxzr.youyou.fragment.VipFragment;
import com.yxzr.youyou.retrofit.BaseObserver;
import com.yxzr.youyou.retrofit.RequestUtil;
import com.yxzr.youyou.utils.AppManager;
import com.yxzr.youyou.utils.NullStringToEmptyAdapterFactory;
import com.yxzr.youyou.utils.SharedPreferencesHelper;
import com.yxzr.youyou.utils.StatusBarUtil;
import com.yxzr.youyou.utils.StatusUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    private RadioGroup mRadioGroup;
    private RadioButton radiobutton1,radiobutton2,radiobutton3;
    private FragmentTransaction beginTransaction;
    private FragmentManager fragmentManager;
    private HomeFragment homeFragment;
    private VipFragment vipFragment;
    private MyFragment myFragment;
    private StatusUtils statusUtils;
    private int index = 0;
    private SharedPreferencesHelper sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StatusBarUtil.setTranslucentStatus(this);
        AppManager.getInstance().addActivity(this); //添加到栈中
        statusUtils = new StatusUtils(this);
        sp = new SharedPreferencesHelper(this,"logindata");
        initView();
    }

    private void initView() {
        mRadioGroup = findViewById(R.id.radioGroup);
        radiobutton1 = findViewById(R.id.home_radiobutton);
        radiobutton2 = findViewById(R.id.vip_radiobutton);
        radiobutton3 = findViewById(R.id.my_radiobutton);

        homeFragment = new HomeFragment();
        vipFragment = new VipFragment();
        myFragment = new MyFragment();

        fragmentManager = getSupportFragmentManager();
        //默认显示首页
        beginTransaction = fragmentManager.beginTransaction();
        beginTransaction.replace(R.id.home_frame,homeFragment);
        beginTransaction.commit();

        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch(i){
                    case R.id.home_radiobutton:
                        //新开一个事务
                        index = 0;
                        beginTransaction = fragmentManager.beginTransaction();
                        beginTransaction.replace(R.id.home_frame,homeFragment);
                        beginTransaction.commit();
                        break;
                    case R.id.vip_radiobutton:
                        if (statusUtils.isLogin()){
                            initData();
                        }else {
                            radiobutton1.setChecked(true);
                            statusUtils.toLoginActivity();
                        }

                        break;
                    case R.id.my_radiobutton:
                        if (statusUtils.isLogin()){
                            beginTransaction = fragmentManager.beginTransaction();
                            beginTransaction.replace(R.id.home_frame,myFragment);
                            beginTransaction.commit();
                        }else {
                            radiobutton1.setChecked(true);
                            statusUtils.toLoginActivity();
                        }
                        break;
                    default:
                        break;
                }
            }
        });
    }

    private void initData(){
        RequestUtil.getInstence().API().getUserInfo((String) sp.getSharedPreference("token",""))
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(this,true){
                    @Override
                    protected void onSuccees(String t) {
                        Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
                        UserInfoBean bean = gson.fromJson(t,UserInfoBean.class);
                        if ("0".equals(bean.getData().getVip())){
                            Toast.makeText(MainActivity.this,"您不是会员请充值",Toast.LENGTH_LONG).show();
                        }else {

                            beginTransaction = fragmentManager.beginTransaction();
                            beginTransaction.replace(R.id.home_frame,vipFragment);
                            beginTransaction.commit();
                        }
                    }

                    @Override
                    protected void onFail(int error_code) {

                    }

                    @Override
                    protected void onComp() {

                    }

                    @Override
                    protected void onErr(Throwable e) {

                    }
                });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getInstance().finishActivity(this); //从栈中移除
    }
}
